package com.xlhj.boot.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: caijliu
 * @Date: 04/09/2021 15:38
 * @Description 延迟队列(插件实现)配置类
 * @Vsersion: 0.0.1
 */
@Configuration
public class RabbitDelayedConfig {

    private static final String DELAYED_EXCHANGE_NAME = "delayed.exchange";
    private static final String DELAYED_QUEUE_NAME = "delayed.queue";
    private static final String DELAYED_ROUT_KEY = "delayed.routingkey";

    @Bean(name = "delayedExchange")
    public CustomExchange delayedExchange() {
        Map<String, Object> map = new HashMap<>(1);
        map.put("x-delayed-type", "direct");
        return new CustomExchange(DELAYED_EXCHANGE_NAME, "x-delayed-message", true, false, map);
    }

    @Bean(name = "delayedQueue")
    public Queue delayedQueue() {
        return new Queue(DELAYED_QUEUE_NAME);
    }

    @Bean
    public Binding bindingDelayedQueue(@Qualifier("delayedQueue") Queue queue, @Qualifier("delayedExchange") CustomExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(DELAYED_ROUT_KEY).noargs();
    }
}
